package components.borders;

import java.awt.Color;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.appearances.Rectangle;

import components.CollisionableComponent;
import components.asteroids.Asteroid;
import components.scenes.MainGameScene;
import components.ship.Ship;
import components.weapon.Proyectile;

public class PortBorder extends CollisionableComponent<MainGameScene> {

	public static int SPAWNTOP = 0;
	public static int SPAWNBOT = 1;
	public static int SPAWNLEFT = 2;
	public static int SPAWNRIGHT = 4;
	
	private PortBorder targetBorder;
	private int spawnSide;
	
	public PortBorder(int aSpawnSide, int aDiameter, int aHeight, double x, double y) {
		super(aDiameter, aHeight, x, y);
		this.setAppearance(new Rectangle(Color.BLACK, aDiameter, aHeight));
		this.spawnSide = aSpawnSide;
	}

	@Override
	public boolean isSolid() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected void onCollisionWithSolid(CollisionableComponent gc) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void defineCollide(CollisionableComponent gc) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void effectiveUpdate(DeltaState deltaState) {
		// TODO Auto-generated method stub
		
	}
	
	public void collideWithShip(Ship ship) {
		this.getTargetBorder().spawn(ship);
	}

	public void collideWithProyectile(Proyectile proyectile) {
		proyectile.destroyComponent();
	}
	
	public void collideWithAsteroid(Asteroid asteroid) {
		this.getTargetBorder().spawn(asteroid);
	}

	
	public void spawn (CollisionableComponent cp){
		if (this.getSpawnSide() == PortBorder.SPAWNBOT){
			cp.setY(this.getY() + this.getHeight() + 1);
		}
		else if(this.getSpawnSide() == PortBorder.SPAWNTOP){
			cp.setY(this.getY() - cp.getHeight()- 1);
		}
		else if(this.getSpawnSide() == PortBorder.SPAWNLEFT){
			cp.setX(this.getX() - cp.getDiameter() - 1);
		}
		else if(this.getSpawnSide() == PortBorder.SPAWNRIGHT){
			cp.setX(this.getX() + this.getDiameter()+  1);
		}
		else {
			throw new RuntimeException("bad spawn code");
		}
	}

	public PortBorder getTargetBorder() {
		return targetBorder;
	}

	public void setTargetBorder(PortBorder targetBorder) {
		this.targetBorder = targetBorder;
	}
	public int getSpawnSide() {
		return spawnSide;
	}

	public void setSpawnSide(int spawnSide) {
		this.spawnSide = spawnSide;
	}

}
